package cn.bdqn.pn.controller.user;

import cn.bdqn.pn.pojo.user.User;
import cn.bdqn.pn.service.user.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/selectUserInfo")
    public String selectUserInfo(Model model,User user, @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<User> users = null;
        System.out.println("=================status:"+user.getStatus());
        System.out.println("=================nickName:"+user.getNickName());
        try {
            users = userService.selectUserInfo(user, pageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
            /*return JSON.toJSONString(users,true);*/
        }
        System.out.println(JSON.toJSONString(users, true));
        model.addAttribute("nickName",user.getNickName());
        model.addAttribute("status",user.getStatus());
        model.addAttribute("telephone",user.getTelephone());
        model.addAttribute("page",users);
        System.out.println(users.getNumber());
        return "member_list";
    }

    @RequestMapping(value = "/selectUserDetails")
    public String selectUserDetails(Model model,@RequestParam("userId") Integer userId){
        User userDetail=userService.selectUserDetails(userId);
        model.addAttribute("userDetail",userDetail);
        System.out.println(userDetail.getUserIdentity());
        return  "member_list::userinfo_refresh";
    }

    @RequestMapping(value = "/enableUser",method = RequestMethod.POST)
    @ResponseBody
    Integer enableUser(Integer status,Integer userid){
        System.out.println("/************************/"+status);
        System.out.println("/************************/"+userid);
        return userService.enableUser(status,userid);
    }


    /**
     * 统计会员总量
     */
    @RequestMapping("getUsers")
    @ResponseBody
    public Long getUsers() {
        return userService.getUsers();
    }



    /**
     * 统计今日新增会员
     */
    @RequestMapping("getVIPByToday")
    @ResponseBody
    public Integer getVIPByToday(){
        return userService.VIPByToday();
    }

}
